package com.my.study.structures.sort;

/**
 * @author Carlos
 * @version 1.0.0
 * @date 2023/2/23 21:17
 */
public class MyQuickSort {

    public static void main(String[] args) {
        int[] a = {1, 23, 2, 4, 2, 9};
        sort(a, 0, a.length - 1);
        System.out.println(a);   
    }
    
    
    private static void sort(int[] a, int left, int right) {
        if (left < right) {
            int l = left;
            int r = right;
            int pivot = a[l];

            while (l < r) {
                while (l < r && a[r] > pivot) {
                    r--;
                }
                if (l < r) {
                    a[l++] = a[r];
                }
                while (l < r && a[l] < pivot) {
                    l++;
                }
                if (l < r) {
                    a[r--] = a[r];
                }
            }
            a[l] = pivot;
            
            sort(a, left, l-1);
            sort(a, l+1, right);
        }
    }
    
}
